#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class PracticeRcdProvider : CY.GFive.Core.Providers.DALProvider.IPracticeRcdProvider
	{
		#region Sql
		private static readonly string SqlSelectPracticeRcd = "SELECT [Id], [StdCode],[TeacherCode],[StartDate],[EndDate],[EntCode],[ReturnReson],[TeamCate],[TeamPhone],[PratiseState] FROM [PracticeRcd] WHERE  [Id]=@Id";

		private static readonly string SqlInsertPracticeRcd = "INSERT INTO [PracticeRcd]([StdCode],[TeacherCode],[StartDate],[EndDate],[EntCode],[ReturnReson],[TeamCate],[TeamPhone],[PratiseState]) values(@StdCode,@TeacherCode,@StartDate,@EndDate,@EntCode,@ReturnReson,@TeamCate,@TeamPhone,@PratiseState);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdatePracticeRcd = "UPDATE [PracticeRcd] SET [StdCode] = @StdCode,[TeacherCode] = @TeacherCode,[StartDate] = @StartDate,[EndDate] = @EndDate,[EntCode] = @EntCode,[ReturnReson] = @ReturnReson,[TeamCate] = @TeamCate,[TeamPhone] = @TeamPhone,[PratiseState] = @PratiseState WHERE  [Id]=@Id";

		private static readonly string SqlDeletePracticeRcd = "DELETE [PracticeRcd] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllPracticeRcd = "SELECT [Id], [StdCode],[TeacherCode],[StartDate],[EndDate],[EntCode],[ReturnReson],[TeamCate],[TeamPhone],[PratiseState] FROM [PracticeRcd]";
		#endregion

		#region IPracticeRcdProvider Members
		public Core.Business.PracticeRcd Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectPracticeRcd);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.PracticeRcd practiceRcd = new Core.Business.PracticeRcd();

				if (!reader.IsDBNull(0)) practiceRcd.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) practiceRcd.StdCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) practiceRcd.TeacherCode = reader.GetString(2);
				if (!reader.IsDBNull(3)) practiceRcd.StartDate = reader.GetDateTime(3);
				if (!reader.IsDBNull(4)) practiceRcd.EndDate = reader.GetDateTime(4);
				if (!reader.IsDBNull(5)) practiceRcd.EntCode = reader.GetString(5);
				if (!reader.IsDBNull(6)) practiceRcd.ReturnReson = reader.GetString(6);
				if (!reader.IsDBNull(7)) practiceRcd.TeamCate = reader.GetString(7);
				if (!reader.IsDBNull(8)) practiceRcd.TeamPhone = reader.GetString(8);
				if (!reader.IsDBNull(9)) practiceRcd.PratiseState = reader.GetString(9);

				return practiceRcd;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.PracticeRcd practiceRcd)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@StdCode", SqlDbType.NVarChar, practiceRcd.StdCode);
			sql.AddParameter("@TeacherCode", SqlDbType.NVarChar, practiceRcd.TeacherCode);
			sql.AddParameter("@StartDate", SqlDbType.DateTime, practiceRcd.StartDate);
			sql.AddParameter("@EndDate", SqlDbType.DateTime, practiceRcd.EndDate);
			sql.AddParameter("@EntCode", SqlDbType.NVarChar, practiceRcd.EntCode);
			sql.AddParameter("@ReturnReson", SqlDbType.NVarChar, practiceRcd.ReturnReson);
			sql.AddParameter("@TeamCate", SqlDbType.NVarChar, practiceRcd.TeamCate);
			sql.AddParameter("@TeamPhone", SqlDbType.NVarChar, practiceRcd.TeamPhone);
			sql.AddParameter("@PratiseState", SqlDbType.NVarChar, practiceRcd.PratiseState);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertPracticeRcd);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) practiceRcd.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.PracticeRcd practiceRcd)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@StdCode", SqlDbType.NVarChar, practiceRcd.StdCode);
			sql.AddParameter("@TeacherCode", SqlDbType.NVarChar, practiceRcd.TeacherCode);
			sql.AddParameter("@StartDate", SqlDbType.DateTime, practiceRcd.StartDate);
			sql.AddParameter("@EndDate", SqlDbType.DateTime, practiceRcd.EndDate);
			sql.AddParameter("@EntCode", SqlDbType.NVarChar, practiceRcd.EntCode);
			sql.AddParameter("@ReturnReson", SqlDbType.NVarChar, practiceRcd.ReturnReson);
			sql.AddParameter("@TeamCate", SqlDbType.NVarChar, practiceRcd.TeamCate);
			sql.AddParameter("@TeamPhone", SqlDbType.NVarChar, practiceRcd.TeamPhone);
			sql.AddParameter("@PratiseState", SqlDbType.NVarChar, practiceRcd.PratiseState);
			sql.AddParameter("@Id", SqlDbType.Int, practiceRcd.Id);
			sql.ExecuteSql(SqlUpdatePracticeRcd);
		}

		public void Delete(Core.Business.PracticeRcd practiceRcd)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, practiceRcd.Id);
			sql.ExecuteSql(SqlDeletePracticeRcd);
		}

		public IList<Core.Business.PracticeRcd> GetAllPracticeRcd()
		{
			IList<Core.Business.PracticeRcd> practiceRcdlist = new List<Core.Business.PracticeRcd>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllPracticeRcd);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.PracticeRcd practiceRcd = new Core.Business.PracticeRcd();

					if (!reader.IsDBNull(0)) practiceRcd.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) practiceRcd.StdCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) practiceRcd.TeacherCode = reader.GetString(2);
					if (!reader.IsDBNull(3)) practiceRcd.StartDate = reader.GetDateTime(3);
					if (!reader.IsDBNull(4)) practiceRcd.EndDate = reader.GetDateTime(4);
					if (!reader.IsDBNull(5)) practiceRcd.EntCode = reader.GetString(5);
					if (!reader.IsDBNull(6)) practiceRcd.ReturnReson = reader.GetString(6);
					if (!reader.IsDBNull(7)) practiceRcd.TeamCate = reader.GetString(7);
					if (!reader.IsDBNull(8)) practiceRcd.TeamPhone = reader.GetString(8);
					if (!reader.IsDBNull(9)) practiceRcd.PratiseState = reader.GetString(9);

					practiceRcd.MarkOld();
					practiceRcdlist.Add(practiceRcd);
				}
				reader.Close();
			}
			return practiceRcdlist;
		}
		#endregion
	}
}
